//
//  PhoneViewController.m
//  NuwaRobot
//
//  Created by Kendama on 2017/12/2.
//  Copyright © 2017年 Kendama. All rights reserved.
//

#import "PhoneViewController.h"
#import "PhoneTableViewCell.h"
#import "DevicePhoneTableViewCell.h"
#import "GetAffectionNumberListApi.h"

@interface PhoneViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong) UITableView *tableView;
@property(nonatomic,strong) NSArray *dataSource;

@end

@implementation PhoneViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self getAffectionNumberList];
}

#pragma mark --- 事件处理
- (void)getAffectionNumberList
{
    GetAffectionNumberListApi *api = [[GetAffectionNumberListApi alloc] initWithDic:@{@"sn":EXRACT_OBJECT(DEVICEMAC)}];
    [api dtStartYTKRequestCompletionBlockWithSuccess:^(YTKBaseRequest * _Nonnull request) {
        NSDictionary *dict = [request responseJSONObject];
        NSMutableArray *array = [NSMutableArray arrayWithCapacity:3];
        [array addObject:@{@"fullname":@"设备电话",@"mobile":@"13715881562",@"id":@"1"}];
        [array addObjectsFromArray:dict[@"data"]];
        self.dataSource = array;
        [self.tableView reloadData];
    } failure:nil];
}

- (void)callToPhone:(NSString *)phone
{
    NSMutableString *str=[[NSMutableString alloc] initWithFormat:@"tel:%@",phone];
    UIWebView *callWebview = [[UIWebView alloc] init];
    [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
    [self.view addSubview:callWebview];
}

#pragma mark --- tabelview代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataSource.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        return px1920Height(300);

    }
    else
    {
        return px1920Height(180);

    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return px1920Height(30);
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    __weak typeof(self) weakSelf = self;
    if (indexPath.section == 0)
    {
        DevicePhoneTableViewCell *cell = [DevicePhoneTableViewCell cellWithTableView:tableView forIndexPath:indexPath];
        cell.titleLabel.text = self.dataSource[indexPath.section][@"fullname"];
        return cell;
    }
    else
    {
        PhoneTableViewCell *cell = [PhoneTableViewCell cellWithTableView:tableView forIndexPath:indexPath];
        cell.nameLabel.text = self.dataSource[indexPath.section][@"fullname"];
        cell.phoneLabel.text = self.dataSource[indexPath.section][@"mobile"];
        cell.CallBlock = ^{
            [weakSelf callToPhone:weakSelf.dataSource[indexPath.section][@"mobile"]];
        };
        return cell;
    }

    
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];

    
}

#pragma mark --- 懒加载
- (UITableView *)tableView
{
    if(!_tableView)
    {
        UIView *superView = self.view;
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStyleGrouped];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        [superView addSubview:_tableView];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.scrollEnabled = NO;
    }
    return _tableView;
}

//- (NSArray *)dataSource
//{
//    if(!_dataSource)
//    {
//        _dataSource = @[@"设备电话",@"妈妈",@"姐姐",@"舅舅"];
//    }
//    return _dataSource;
//}


#pragma mark --- 设置视图布局
- (void)masLayoutSubview
{
    __weak UIView *superView = self.view;
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(superView.mas_top).with.offset(px1920Height(252));
        make.left.mas_equalTo(superView.mas_left).with.offset(px1080Width(40));
        make.right.mas_equalTo(superView.mas_right).with.offset(-px1080Width(40));
        make.bottom.mas_equalTo(superView);
    }];
    
}

- (void)setNavigationStyle
{
    [self setNavigationBarTitle:@"拨打电话"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
